package zyc.web;

import zyc.pojo.User;
import zyc.service.UserService;
import zyc.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * @description:
 * @author: ZhaoYicong
 * @date: Created in 2020/7/17 9:55
 * @version: v1.0
 * @modified By:
 */
public class UserServlet extends BaseServlet {

    /**
     * @descript :登录功能的操作
     * @author :ZhaoYicong
     * @date :2020/7/17 10:06
     * @Param: null
     * @return :
     * @throws :
     * @since :
     */


    public void login(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        UserService userService=new UserServiceImpl();
        User user = userService.login(new User(null, username, password, null));
        if (user==null){
            req.setAttribute("masg","登录失败用户名或者密码错误");
            req.setAttribute("username",username);

            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
        }else{
            System.out.println("登录成功！");

            req.getSession().setAttribute("username",username);

            req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp);
        }
    }

    /**
     * @descript :注册功能的模块
     * @author :ZhaoYicong
     * @date :2020/7/17 10:07
     * @Param: null
     * @return :
     * @throws :
     * @since :
     */

    public void regist(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String username = req.getParameter("username");
        System.out.println(username);
        String password = req.getParameter("password");
        String email = req.getParameter("email");

        String token =(String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);

        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);

        String code = req.getParameter("code");
        System.out.println(code);

        User user=new User(null,username,password,email);

        UserService userService=new UserServiceImpl();

        if (token.equals(code)){

            boolean b = userService.exsitsUser(username);
            if (b){
                req.setAttribute("masg","用户名已经存在");

                req.setAttribute("username",username);
                req.setAttribute("email",email);
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);

            }else{

                req.getSession().setAttribute("username",username);

                userService.registUser(user);
                req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req,resp);
            }
        }else{
            req.setAttribute("masg","验证码错误");
            req.setAttribute("username",username);
            req.setAttribute("email",email);
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);
        }

    }


    protected void logout (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getSession().invalidate();
        System.out.println("111"+req.getContextPath());
        resp.sendRedirect(req.getContextPath());
    }


//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//
//        String action=req.getParameter("action");
//
//
//        try {
//            Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
//            method.invoke(this,req,resp);
//        } catch (Exception e) {
//            System.out.println(e);
//        }
////        if ("login".equals(action)){
////            login(req,resp);
////
////        }else if ("regist".equals(action)){
////
////            regist(req, resp);
////
////        }
//    }
}
